﻿Namespace PL.TSMSDK
    Public Class TimeSheetHour_PL
        Inherits PLBase
        Implements IPLProcess

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating

        End Sub
        Sub OnRowUpdated(ByVal sender As Object, ByVal e As SqlRowUpdatedEventArgs) Handles MetaDataTable.OnRowUpdated
            'If e.Status = UpdateStatus.ErrorsOccurred Then
            '    e.Row.RowError = e.Errors.Message
            '    e.Status = UpdateStatus.SkipCurrentRow
            'End If
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            Return Not Me.MetaDataTable.HasErrors
        End Function

        Public Overrides Sub ValidateRow(ByVal row As DataRow)
            Dim projectID As Guid? = IIf(IsDBNull(row("ProjectID_FK")), Nothing, row("ProjectID_FK"))
            Dim dateWorked As Date? = IIf(IsDBNull(row("DateWorked", DataRowVersion.Proposed)), Nothing, row("DateWorked", DataRowVersion.Proposed))

            Dim oldDateWorked As Date? = Nothing
            If row.HasVersion(DataRowVersion.Original) Then
                oldDateWorked = IIf(IsDBNull(row("DateWorked", DataRowVersion.Original)), Nothing, row("DateWorked", DataRowVersion.Original))
            End If
            Dim beginDate As Date?
            Dim endDate As Date?
            Dim projectName As String

            row.ClearErrors()

            'Pour le Middleman... Regle SCIEF
            'If row("WorkDescription").ToString.Length < 8 Then
            '    'TODO: Get metastring ex:
            '    row.SetColumnError("WorkDescription", "Please Complete Your Work Description, 8 Characters Minimum")
            'End If
            '
            If projectID Is Nothing Then
                'TODO: Get metastring ex:
                row.SetColumnError("ProjectID_FK", "Please Complete Your Project")
            ElseIf dateWorked IsNot Nothing AndAlso oldDateWorked <> dateWorked Then
                Using mq As MetaQuery = MetaQuery.GetMetaQueryByName("Project")
                    mq.SQLWhere = String.Format("ProjectId = '{0}'", projectID.ToString)
                    mq.FillResultQuery()
                    If mq.ResultQuery.Rows.Count = 0 Then
                        'TODO: Get metastring ex:
                        row.SetColumnError("ProjectID_FK", "Invalid Project")
                    Else
                        beginDate = IIf(IsDBNull(mq.ResultQuery.Rows(0).Item("ProjectBeginDate")), Nothing, mq.ResultQuery.Rows(0).Item("ProjectBeginDate"))
                        endDate = IIf(IsDBNull(mq.ResultQuery.Rows(0).Item("ProjectEndDate")), Nothing, mq.ResultQuery.Rows(0).Item("ProjectEndDate"))
                        projectName = mq.ResultQuery.Rows(0).Item("ProjectName")
                        If dateWorked < beginDate OrElse dateWorked > endDate Then
                            'TODO: Get metastring ex:
                            row.SetColumnError("DateWorked", String.Format("The ""Date Worked"" field value must be between {0} and {1} inclusively for project {2}", beginDate, endDate, projectName))
                        End If
                    End If
                End Using
            ElseIf dateWorked Is Nothing Then
                'TODO: Get metastring ex:
                row.SetColumnError("DateWorked", "Please Complete Your DateWorked")
            End If
            '
            If IsDBNull(row("WorkCodeID_FK")) Then
                'TODO: Get metastring ex:
                row.SetColumnError("WorkCodeID_FK", "Please Complete Your WorkCode")
            End If
            '
            If IsDBNull(row("Billable")) Then
                'TODO: Get metastring ex:
                row.SetColumnError("Billable", " Mandatory")
            End If
            '
            If IsDBNull(row("Payable")) Then
                'TODO: Get metastring ex:
                row.SetColumnError("Payable", " Mandatory")
            End If
            '
            If row.HasErrors Then
                'TODO: Get metastring ex:
                row.RowError = "Please Check Your Columns, a Validation Error Occur"
            End If
        End Sub

        Public Overridable Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Overridable Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Overridable Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            dataRow("TimeCardDetailID") = System.Guid.NewGuid
            Return True
        End Function
        ''' <summary>
        ''' This will copy an timesheetHour line to a new line with exact same info.
        ''' </summary>
        ''' <param name="TimeCardDetailID"></param>
        ''' <param name="newName"></param>
        ''' <remarks></remarks>
        Shared Sub CopyActivity(TimeCardDetailID As Guid, newName As String)
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("fspCopyTimeSheetHourLine")
                '
                sqlcmd.Parameters("@SourceTimeCardDetailID").Value = TimeCardDetailID

                '
                sqlcmd.ExecuteNonQuery()
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Sub


    End Class
End Namespace
